#include "player.h"


Player::Player() : MovingUnit()
{
    init();
}

Player::Player(QRect &geometry) : MovingUnit(geometry)
{
    init();
}


void Player::init()
{
    m_leftSprite = "left";
    m_upSprite = "up";
    m_rightSprite = "right";
    m_downSprite = "down";

    loadPixmaps();

    setLives(3);
    m_immortal = 0;
}

int Player::getType() const {return Unit::Player;}

void Player::loseALife()
{
    if(m_immortal || getLives() <= 0) return;

    setLives(getLives() -1);
    m_immortal = 60;

}


void Player::draw(QPainter *painter)
{
    /*
    painter->fillRect(this->m_geometry, Qt::blue);

    painter->setPen(Qt::white);
    switch (getDirection())
    {
        case Left: painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "L"); break;
        case Up: painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "U"); break;
        case Right: painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "R"); break;
        case Down: painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "D"); break;
        default: ;
    }
    */

    painter->drawPixmap(m_geometry, m_pixmaps[m_direction].at(m_step));
}

void Player::advance()
{
    if (m_immortal > 0) m_immortal--;
}

void Player::serializeDebug(QDebug &dbg)
{
    dbg << "Player ("
            << "immortal:" << m_immortal;
    MovingUnit::serializeDebug(dbg);
    dbg << ")";
}

void Player::serializeIn(QDataStream &in)
{
    MovingUnit::serializeIn(in);
    in >> m_immortal;
}

void Player::serializeOut(QDataStream &out)
{
    MovingUnit::serializeOut(out);
    out << (qint32)m_immortal;
}
